﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using SuntabuAssistant.AssistAchieve.Action;
using SuntabuAssistant.AssistAchieve.Hook;
using SuntabuAssistant.Voice2Redmine.KeyControlRec;
using SuntabuAssistant.Voice2Redmine.Recognise;
using MessageBox = System.Windows.Forms.MessageBox;

namespace SuntabuAssistant.AssistAchieve
{
    public class Assistant : Singleton<Assistant>
    {
        protected static Timer AssistTimer;

        protected bool WorkState = false;
        KeyboardHook keyboardHook = new KeyboardHook();
        private Assistant()
        {
            AssistTimer = new Timer { Interval = 1000 };
            AssistTimer.Start();
            AssistTimer.Tick += AssistTimer_Tick;
        }

        public int Times { get; set; }

        public bool IsZhun { get; set; }

        public delegate void HideWin(int times);
        public HideWin Hide;///实例化一个委托，在其他地方讲其他地方的方法作为参数传入

        
        private void AssistTimer_Tick(object sender, EventArgs e)
        {
            if (WorkState)
            {
                if (Times > 10000)
                {
                    Times = 0;
                }



                //  Disturber.IntervalDialog(times);

                Rectangle rec = Screen.PrimaryScreen.Bounds;
               // CaptureScreen.GetImage(rec.Width, rec.Height);

                //Action.Volume.SetMute();

              //  Action.Volume.SetVolume(1);
            }

            Times++;
            Dispatcher.CurrentDispatcher.BeginInvoke(Hide, Times);



        }

        /// <summary>
        /// 
        /// </summary>
        public void StartWorking()
        {
            WorkState = true;
            InitHook();
        }

        public void StopWorking()
        {
            WorkState = false;
        }

     
        void InitHook()
        {
            keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
            keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
            keyboardHook.Start();
        }

        void keyboardHook_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.RShiftKey:
                    { 
                      var dataPipe=  VoiceControl.Instance.VoiceRecogise();
                      if (dataPipe != null)
                      {
                          // MessageBox.Show(dataPipe.Recognised); 
                          var cmd = dataPipe.Recognised;
                          if (cmd.Contains("静音"))
                          {
                              Action.Volume.SetMute();
                          }
                          else if (cmd.Contains("声音最大"))
                          {
                              Action.Volume.SetVolume(1);
                          } 
                      }

                    }
                    break;

            }
        }
        void keyboardHook_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.RShiftKey:
                    {
                        

                        VoiceControl.Instance.VoiceRecord();
                    }
                    break;
                
            }
            
        }
    }
}